/**
 * This file is part of Wireless TransFer.
 *
 *   Wireless TransFer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *	
 *   Wireless TransFer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Wireless TransFer.  If not, see <http://www.gnu.org/licenses/>.
 *
 *	 Copyright (C) 2011
 *   @author prebz
 */

package com.wirelesstransfer.activity.receive;

import java.io.File;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.wirelesstransfer.Main;
import com.wirelesstransfer.R;
import com.wirelesstransfer.activity.dialogs.Question;
import com.wirelesstransfer.activity.filebrowser.FileBrowser;
import com.wirelesstransfer.client.ApacheClient;
import com.wirelesstransfer.client.ClientConnection;
import com.wirelesstransfer.server.WIFI.handlers.Files;

public class FileList extends FileBrowser implements OnClickListener{
	
	private ApacheClient client;
	private ClientConnection clientConnection;
	
	private Resources res;
	
	private Files files;
	private List<File> allFiles;
		
	// Dialog constants
	public static final int DIALOG_DECLINE = 0;
	public static final int DIALOG_ACCEPT = 1;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filebrowser);
        
		res = getResources();
		
		client = ApacheClient.getInstance();
		
//		files = Files.getInstance();
		allFiles = client.getConnection().getTransferData().getFiles();
	        
        // Click listeners for buttons
		View acceptButton = findViewById(R.id.btn_accept);
		acceptButton.setOnClickListener(this);
		View declineButton = findViewById(R.id.btn_decline);
		declineButton.setOnClickListener(this);	
	
		listAllFiles(allFiles);        
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_accept:
			showDialog(DIALOG_ACCEPT);
			break;
		case R.id.btn_decline:
			showDialog(DIALOG_DECLINE);
			break;
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {

		File file = new File(path.get(position));
	
		displayToast(file.getName());

	}	

	@Override
	protected Dialog onCreateDialog (int id){
		Dialog dialog = null;
		
		switch (id) {
		case DIALOG_DECLINE:
			dialog = buildCancelDialog(res.getString(R.string.filelist_dialog_decline));
			break;
		case DIALOG_ACCEPT:
			dialog = buildAcceptDialog(res.getString(R.string.filelist_dialog_accept));
			break;
		default:
			dialog = null;
			break;
		}
		return dialog;
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.questionmenu, menu);
        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	    case R.id.question_menu:
	    	Intent question = new Intent(this, Question.class);
	    	question.putExtra(res.getString(R.string.question_TITLE),res.getString(R.string.question_file_list_title_txt));
	    	question.putExtra(res.getString(R.string.question_TEXT),res.getString(R.string.question_file_list_txt));
			startActivity(question);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	private AlertDialog buildCancelDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		       .setCancelable(false)
		       .setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   	files.clearFiles();
		                FileList.this.finish(); 
		                clientConnection = client.getConnection();
		                client.disconnect(clientConnection.getSessionId());
		                Intent intent = new Intent(getBaseContext(), Main.class);
		                startActivity(intent);
		           }
		       })
		       .setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		return alert;
	}
	
	/**
	 * Creates text in a dialog with yes/no key
	 * @param action
	 * @return
	 */
	private AlertDialog buildAcceptDialog(String action){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(action)
		       .setCancelable(false)
		       .setPositiveButton(res.getString(R.string.send_dialog_yes), new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   client.selectFiles();
		        	   client.receiveFiles();
		                FileList.this.finish();
		                Intent intent = new Intent(getBaseContext(), Main.class);
		                startActivity(intent);
		           }
		       })
		       .setNegativeButton(res.getString(R.string.send_dialog_no), new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		return alert;
	}
}
